package jt.com.jt809.init;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import jt.com.jt809.config.NettyConfig;
import jt.com.jt809.handler.inbound.ClientByte2MessageInboundHandler;
import jt.com.jt809.handler.inbound.MessageForwardInboundHandler;
import jt.com.jt809.handler.outbound.Message2ByteOutboundHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Netty 客户端处理流程配置
 *
 * @Author OuJiaJun
 * @Date 2024/4/24 14:44
 */
@Component
public class JT809ClientChannelInit extends ChannelInitializer<SocketChannel> {
    @Resource
    private NettyConfig.NettyClientConfig clientConfig;
    @Resource
    private Message2ByteOutboundHandler message2ByteOutboundHandler;
    @Resource
    private ClientByte2MessageInboundHandler clientByte2MessageInboundHandler;
    @Resource
    private MessageForwardInboundHandler messageForwardInboundHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline().addLast(new IdleStateHandler(clientConfig.getReaderIdleTimeSeconds(),clientConfig.getWriterIdleTimeSeconds(),clientConfig.getAllIdleTimeSeconds(), TimeUnit.SECONDS));
        socketChannel.pipeline().addLast(message2ByteOutboundHandler);
        socketChannel.pipeline().addLast(clientByte2MessageInboundHandler);
        socketChannel.pipeline().addLast(messageForwardInboundHandler);
    }
}
